home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtool17.zip / TVTOOLS.ZIP / ASCII.CPP < prev    next >
C/C++ Source or Header  |  1993-01-14  |  5KB  |  271 lines

  1. #define Uses_TAsciiChart
  2. #define Uses_TEvent
  3. #define Uses_TStreamable
  4. #define Uses_TStreamableClass
  5. #include "tvtools.h"
  6. __link( RView )
  7. __link( RWindow )
  8.  
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12. #include <strstrea.h>
  13. #include <iomanip.h>
  14.  
  15.  
  16. //
  17. // TTable functions
  18. //
  19.  
  20. const char * const TTable::name = "TTable";
  21.  
  22.  
  23. void TTable::write( opstream& os )
  24. {
  25.     TView::write( os );
  26. }
  27.  
  28.  
  29. void *TTable::read( ipstream& is )
  30. {
  31.     TView::read( is );
  32.     return this;
  33. }
  34.  
  35.  
  36. TStreamable *TTable::build()
  37. {
  38.     return new TTable( streamableInit );
  39. }
  40.  
  41.  
  42. TStreamableClass RTable( TTable::name,
  43.              TTable::build,
  44.              __DELTA(TTable)
  45.                );
  46.  
  47.  
  48. TTable::TTable(TRect& r) :
  49.  TView( r )
  50. {
  51. }
  52.  
  53.  
  54. void TTable::draw()
  55. {
  56.     TDrawBuffer buf;
  57.     char        color = getColor(6);
  58.  
  59.     for(int y = 0; y <= size.y-1; y++)
  60.     {
  61.     buf.moveChar(0, ' ', color, size.x);
  62.     for(int x = 0; x <= size.x-1; x++)
  63.         buf.moveChar(x, 32*y+x, color, 1);
  64.     writeLine(0, y, size.x, 1, buf);
  65.     }
  66.     showCursor();
  67. }
  68.  
  69. //
  70. // cmCharFocused is a offset value (basically the ascii code of the
  71. // current selected character) thus should be added, not or'ed, to
  72. // cmAsciiTableCmdBase.
  73. //
  74.  
  75. void TTable::charFocused()
  76. {
  77.     message(owner, evBroadcast, cmAsciiTableCmdBase + cmCharFocused,
  78.       (void *) (cursor.x + 32 * cursor.y));
  79. }
  80.  
  81.  
  82. void TTable::handleEvent(TEvent& event)
  83. {
  84.     TView::handleEvent(event);
  85.  
  86.     if (event.what == evMouseDown)
  87.     {
  88.     do
  89.         {
  90.         if(mouseInView(event.mouse.where))
  91.         {
  92.         TPoint spot = makeLocal(event.mouse.where);
  93.         setCursor(spot.x, spot.y);
  94.         charFocused();
  95.         }
  96.         } while (mouseEvent(event, evMouseMove));
  97.     clearEvent(event);
  98.     }
  99.     else
  100.     {
  101.     if (event.what == evKeyboard)
  102.         {
  103.         switch (event.keyDown.keyCode)
  104.         {
  105.         case kbHome:
  106.             setCursor(0,0);
  107.             break;
  108.         case kbEnd:
  109.             setCursor(size.x-1, size.y-1);
  110.             break;
  111.         case kbUp:
  112.             if (cursor.y > 0)
  113.             setCursor(cursor.x, cursor.y-1);
  114.             break;
  115.         case kbDown:
  116.             if (cursor.y < size.y-1)
  117.             setCursor(cursor.x, cursor.y+1);
  118.             break;
  119.         case kbLeft:
  120.             if (cursor.x > 0)
  121.             setCursor(cursor.x-1, cursor.y);
  122.             break;
  123.         case kbRight:
  124.             if (cursor.x < size.x-1)
  125.             setCursor(cursor.x+1, cursor.y);
  126.                     break;
  127.         default:
  128.                     setCursor(event.keyDown.charScan.charCode % 32,
  129.                       event.keyDown.charScan.charCode / 32);
  130.                     break;
  131.                 }
  132.             charFocused();
  133.             clearEvent(event);
  134.         }
  135.         }
  136. }
  137.  
  138.  
  139. //
  140. // TReport functions
  141. //
  142.  
  143. const char * const TReport::name = "TReport";
  144.  
  145.  
  146. void TReport::write( opstream& os )
  147. {
  148.     TView::write( os );
  149.     os << asciiChar;
  150. }
  151.  
  152.  
  153. void *TReport::read( ipstream& is )
  154. {
  155.     TView::read( is );
  156.     is >> asciiChar;
  157.     return this;
  158. }
  159.  
  160.  
  161. TStreamable *TReport::build()
  162. {
  163.     return new TReport( streamableInit );
  164. }
  165.  
  166.  
  167. TStreamableClass RReport( TReport::name,
  168.               TReport::build,
  169.               __DELTA(TReport)
  170.             );
  171.  
  172.  
  173. TReport::TReport(TRect& r) :
  174.  TView(r)
  175. {
  176.     asciiChar = 0;
  177. }
  178.  
  179.  
  180. void TReport::draw()
  181. {
  182.     TDrawBuffer buf;
  183.     char        color = getColor(6);
  184.     char        str[80];
  185.     ostrstream  statusStr( str, sizeof str );
  186.  
  187.     statusStr
  188.       << "  Char: " << ((asciiChar == 0) ? (char) 0x20 : (char) asciiChar)
  189.       << " Decimal: " << setw(3) << (int) asciiChar
  190.       << " Hex " << hex << setiosflags(ios::uppercase)
  191.       << setw(2) << (int) asciiChar << "     " << ends;
  192.  
  193.     buf.moveStr(0, str, color);
  194.     writeLine(0, 0, 32, 1, buf);
  195. }
  196.  
  197.  
  198. void TReport::handleEvent(TEvent& event)
  199. {
  200.     TView::handleEvent(event);
  201.     if (event.what == evBroadcast)
  202.     {
  203.         if (event.message.command == cmAsciiTableCmdBase + cmCharFocused)
  204.             {
  205.         asciiChar = event.message.infoLong;
  206.         drawView();
  207.             }
  208.         }
  209. }
  210.  
  211.  
  212. //
  213. // TAsciiChart functions
  214. //
  215.  
  216. const char * const TAsciiChart::name = "TAsciiChart";
  217.  
  218.  
  219. void TAsciiChart::write( opstream& os )
  220. {
  221.     TWindow::write( os );
  222. }
  223.  
  224.  
  225. void *TAsciiChart::read( ipstream& is )
  226. {
  227.     TWindow::read( is );
  228.     return this;
  229. }
  230.  
  231.  
  232. TStreamable *TAsciiChart::build()
  233. {
  234.     return new TAsciiChart( streamableInit );
  235. }
  236.  
  237.  
  238. TStreamableClass RAsciiChart( TAsciiChart::name,
  239.                   TAsciiChart::build,
  240.                   __DELTA(TAsciiChart)
  241.                 );
  242.  
  243.  
  244. TAsciiChart::TAsciiChart() :
  245.     TWindow(TRect(0, 0, 34, 12), "ASCII Chart", wnNoNumber),
  246.     TWindowInit( &TAsciiChart::initFrame )
  247. {
  248.     TView *control;
  249.  
  250.     flags &= ~(wfGrow | wfZoom);
  251.     palette = wpGrayWindow;
  252.  
  253.     TRect r = getExtent();
  254.     r.grow(-1, -1);
  255.     r.a.y = r.b.y - 1;
  256.     control = new TReport( r );
  257.     control->options |= ofFramed;
  258.     control->eventMask |= evBroadcast;
  259.     insert(control);
  260.  
  261.     r = getExtent();
  262.     r.grow(-1, -1);
  263.     r.b.y = r.b.y - 2;
  264.     control = new TTable( r );
  265.     control->options |= ofFramed;
  266.     control->blockCursor();
  267.     insert(control);
  268.  
  269.     control->select();
  270. }
  271.